package com.hcinfos.gsoo.ui;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.enity.ViewSuppDetail;
import com.hcinfos.gsoo.ui.SettingActivity.AuthDialogListener;
import com.hcinfos.gsoo.util.ImageUtil;
import com.hcinfos.gsoo.util.LogUtil;
import com.hcinfos.gsoo.util.ImageUtil.ImageCallback;
import com.hcinfos.gsoo.util.SharePreferenceUtil;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.net.RequestListener;

public class ShareEditActivity extends Activity implements OnClickListener {

	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;
	private EditText shareContent;

	private ImageView content_img;
	private static final String CONSUMER_KEY = "2912786215";// 替换为开发者的appkey，例如"1646212860";2912786215
	private static final String REDIRECT_URL = "http://www.sina.com";
	private Weibo mWeibo;
	private SharePreferenceUtil util;
	private Button shareBt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		util = new SharePreferenceUtil(this, "setting");
		setContentView(R.layout.activity_share_edit);
		initRes();
		mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
	}

	private void initRes() {
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("新浪分享");

		left_ib = (ImageButton) findViewById(R.id.left_ib);
		left_ib.setOnClickListener(this);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		reg_tv.setVisibility(View.GONE);

		shareContent = (EditText) findViewById(R.id.shareContent);
		content_img = (ImageView) findViewById(R.id.content_img);
		shareBt = (Button) findViewById(R.id.shareBt);
		shareBt.setOnClickListener(this);
	}

	private String token;
	private String expires_in;
	private String url;

	@Override
	protected void onResume() {
		super.onResume();
		String name = util.loadStringSharedPreference("weibo_name");
		if (name != null) {
			center_tv.setText(name);
		}
		token = util.loadStringSharedPreference("token");
		expires_in = util.loadStringSharedPreference("expires_in");

		ViewSuppDetail detail = (ViewSuppDetail) getIntent()
				.getSerializableExtra("shopDetail");
		if (detail == null)
			return;
		String str = "这里是#" + detail.getSuppName() + "#:#即搜中国#";
		shareContent.setText(str);
		url = Constants.SERVER + detail.getImgAddr() + detail.getImgId()
				+ ".jpg";
		LogUtil.d("wxf", "share:url" + url);
		ImageUtil.setThumbnailView(url, content_img, ShareEditActivity.this,
				new ImageCallback() {

					@Override
					public void loadImage(Bitmap bitmap, String imagePath) {
						content_img.setBackgroundDrawable(new BitmapDrawable(
								bitmap));
					}
				}, true, true);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_ib:
			finish();
			break;
		case R.id.shareBt:
			if (token == null) {
				mWeibo.authorize(ShareEditActivity.this,
						new AuthDialogListener());
			} else {
				sinaShare();
			}
			break;
		}
	}

	private void sinaShare() {
		Oauth2AccessToken access_token = new Oauth2AccessToken(token,
				expires_in);
		StatusesAPI api = new StatusesAPI(access_token);
		api.uploadUrlText(shareContent.getText().toString(), url, null, null,
				new RequestListener() {

					@Override
					public void onIOException(IOException e) {
					}

					@Override
					public void onError(WeiboException e) {
						runOnUiThread(new Runnable() {

							@Override
							public void run() {
								Toast.makeText(ShareEditActivity.this,
										"分享失败!！", Toast.LENGTH_SHORT).show();
							}
						});
					}

					@Override
					public void onComplete(String response) {
						runOnUiThread(new Runnable() {

							@Override
							public void run() {

								Toast.makeText(ShareEditActivity.this,
										"分享成功!！", Toast.LENGTH_SHORT).show();
								finish();
							}
						});
					}
				});
	}

	class AuthDialogListener implements WeiboAuthListener {

		@Override
		public void onComplete(Bundle values) {

			token = values.getString("access_token");
			expires_in = values.getString("expires_in");
			String uid = values.getString("uid");
			util.saveSharedPreferences("token", token);
			util.saveSharedPreferences("expires_in", expires_in);

		}

		@Override
		public void onWeiboException(WeiboException e) {
			Toast.makeText(getApplicationContext(),
					"Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}

		@Override
		public void onError(WeiboDialogError e) {
			Toast.makeText(getApplicationContext(),
					"Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onCancel() {
			Toast.makeText(getApplicationContext(), "Auth cancel : ",
					Toast.LENGTH_LONG).show();
		}
	}

}
